#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置这些按钮的图标
    ui->pushButton_target->setIcon(QIcon(":/img/Payara.png"));
    ui->pushButton_target->setIconSize(QSize(100,100));

    ui->pushButton_up->setIcon(QIcon(":/img/up.png"));
    ui->pushButton_up->setIconSize(QSize(100,100));

    ui->pushButton_down->setIcon(QIcon(":/img/down.png"));
    ui->pushButton_down->setIconSize(QSize(100,100));

    ui->pushButton_left->setIcon(QIcon(":/img/left.png"));
    ui->pushButton_left->setIconSize(QSize(100,100));

    ui->pushButton_right->setIcon(QIcon(":/img/right.png"));
    ui->pushButton_right->setIconSize(QSize(100,100));

    //设置快捷键
    //直接通过按键的名字设置
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));


    //autoRepeat(true)  鼠标连发

    //可以通过案件的枚举设置按键快捷键
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    //ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_W));//通过ctrl+ w控制
    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));

    //开启鼠标点击的连发功能(键盘的连发默认就是支持的)
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}


Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    //获取到target的位置
    QRect rect = ui->pushButton_target->geometry();
    //基于上次的位置设置新的位置
    ui->pushButton_target->setGeometry(rect.x(),rect.y()-10,rect.width(),rect.height());

}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(),rect.y()+10,rect.width(),rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x()-10,rect.y(),rect.width(),rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x()+10,rect.y(),rect.width(),rect.height());
}
